package com.example.demo.controller;

import com.example.demo.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.UUID;

/**
 * @author krystal
 * @date 2023/3/7 22:58
 */
@Slf4j
@RequestMapping("/test")  //路由注册
//@Controller  //程序返回的是数据而非页面
//@ResponseBody  //只有加载的类，别人才能访问
@RestController
public class TestConstroller {

    //@RequestMapping("/hi")
    //@RequestMapping(value = "/hi",method = RequestMethod.POST)
    @GetMapping("/hi")
    public String sayHi(String name,Integer age){
        return "hi"+name+" age:"+age;
    }

    @GetMapping("/num")
    public String getnum(Integer num){
        return "num="+num;
    }

    //接收普通对象
    @GetMapping("/showuser")
    public String showu(User user){
        return user.toString();
    }

    @GetMapping("/showtime")
    public String shoeTime(@RequestParam(value = "t",required = false) String startTime,
                            @RequestParam("t2") String endTime){
        return "开始时间：" +startTime+ " 结束时间：" +endTime;
    }

    //@RequestBody接收JSON对象
    @PostMapping("/show-json-user")
    public String showJSONuser(@RequestBody User user){
        return user.toString();
    }

    @RequestMapping("/login/{username}/{password}")
    public String login(@PathVariable("username")String username,
                         @PathVariable("password")String password){
        return username + ":" + password;
    }

    @RequestMapping("/upfile")
    public String upfile(@RequestPart("myfile")MultipartFile file) throws IOException {
        String path="D:\\baekhyun\\bobo.jpg";
        file.transferTo(new File(path));
        return path;
    }

    @RequestMapping("/finalfile")
    public String finalupfile(@RequestPart("myfile")MultipartFile file) throws IOException {
        //根目录
        String path="D:\\baekhyun\\";
        //根目录+【唯一的文件名】
        path+= UUID.randomUUID().toString();
        //根目录+唯一的文件名+文件的后缀
        path+=file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        file.transferTo(new File(path));
        return path;
    }

    //Spring MVC(Spring Web)内含有HttpServletRequest和HttpServletResponse
    @GetMapping("/getparam")
    public String getparam(HttpServletRequest request){
        return request.getParameter("username");
    }

    //获取全部Cookie
    @RequestMapping("/getck")
    public String getCookie(HttpServletRequest request){
        Cookie[] cookies=request.getCookies();
        for (Cookie item:cookies){
            log.error(item.getName()+":"+item.getValue());
        }
        return "get cookie";
    }

    //获取单个Cookie
    @RequestMapping("/getck2")
    public String getCookie2(@CookieValue("do") String val){
        return "cookie value"+val;
    }

    //获取header
    @RequestMapping("/getheader")
    public String getheader(@RequestHeader("User-Agent") String userAgent){
        return userAgent;
    }

    //存Session
    @RequestMapping("/setsession")
    public String setSession(HttpServletRequest request){
        HttpSession session=request.getSession();
        session.setAttribute("userinfo","userinfo");
        return "set Session success";
    }

    //读Session
    @RequestMapping("getsession")
    public String getSession(HttpServletRequest request){
        HttpSession session=request.getSession(false);
        if (session!=null && session.getAttribute("userinfo")!=null){
            return (String) session.getAttribute("userinfo");
        }else {
            return "暂无session信息";
        }
    }

    @RequestMapping("/getsession2")
    public String getSession2(@SessionAttribute(value = "userinfo",required = false)String userinfo){
        return userinfo;
    }

    @RequestMapping("/respjson")
    public HashMap<String,String> respJson(){
        HashMap<String,String> map=new HashMap<>();
        map.put("baekhyun","baekhyun.value");
        map.put("do","do.value");
        map.put("sehun","sehun.value");
        return map;
    }
}
